<?php
namespace app\rbac\model;
use app\base\model\Base;
use think\Validate;
use think\Db;
class PrivilegeM extends Base
{

       protected function _V($scene,$data)
       {
           $rule = [
               'pri_name' => 'require',
               'module_name' => 'require',
               'controller_name' => 'require',
               'action_name' => 'require',
               'parent_id' => 'require|number',
               'sort_num' => 'require|number',
           ];
        $msg = [
        'pri_name.require' => '权限名称不能为空',
        'module_name.require' => '模块名称不能为空',
        'controller_name.require' => '控制器名称不能为空',
        'action_name.require' => '方法名称不能为空',
        'parent_id.require' => '上级ID不能为空',
        'parent_id.number' => '上级ID只能是数字',
        'sort_num.require' => '排序不能为空',
        'sort_num.number' => '排序只能是数字',
        ];
           $validate = new Validate($rule,$msg);
           $validate->scene('add', ['pri_name', 'module_name','controller_name','action_name','parent_id','sort_num']);
//           $validate->scene('edit', ['admin_name', 'email']);
           $result = $validate->scene($scene)->check($data);
           if($result === false)
           {
               return $validate->getError();
           }
           else
           {
               return true;
           }

       }

    /**
     * 新增数据
     * @return mixed
     */

    public function add($data)
    {
        $res = $this->_V('add',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $res = Db::table('privilege')->insertGetId(['pri_name'=>$data['pri_name'],'module_name'=>$data['module_name'],'controller_name'=>$data['controller_name'],'action_name'=>$data['action_name'],'parent_id'=>$data['parent_id'],'sort_num'=>$data['sort_num'],'ico'=>$data['ico']]);
        if($res)
        {
            return true;
        }
        return false;
    }


    /**
     * 修改数据
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $res = $this->_V('add',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $res = Db::table('privilege')->where('id',$data['id'])->update(['pri_name'=>$data['pri_name'],'module_name'=>$data['module_name'],'controller_name'=>$data['controller_name'],'action_name'=>$data['action_name'],'parent_id'=>$data['parent_id'],'sort_num'=>$data['sort_num'],'ico'=>$data['ico']]);
        if($res)
        {
            return true;
        }
        return false;
    }

    /**
     * 删除数据
     * @param $id
     */
    public function del($id)
    {
       $res = Db::table('privilege')->where('id',$id)->update(['is_usable'=>0]);
        if($res)
        {
            return true;
        }
        else
        {
            $this->code = -1;
            $this->msg = '删除失败';
            return false;
        }
    }



    /***********************************获取分类树*************************************************/
    public function getCatTree(){
        $info=Db::table('privilege')->where('is_usable',1)->order('sort_num asc')->select();
        return $this->_getCatTree($info);
    }
    private function _getCatTree($info,$parent_id=0,$level=0){
        static $data=array();
        foreach($info as $k=>$v){
            if($v['parent_id']==$parent_id){
                $v['level']=$level;
                $data[]=$v;
                $this->_getCatTree($info,$v['id'],$level+1);
            }
        }
        return $data;
    }
    /***********************************获取一个分类的子分类****************************************/
    public function getCatChild($pri_id){
        $info=Db::table('privilege')->where('is_usable',1)->select();
        return $this->_getCatChild($info,$pri_id,TRUE);
    }
    private function _getCatChild($info,$parent_id,$isClear=FALSE){
        static $data=array();
        if($isClear)
            $data=array();
        foreach($info as $k=>$v){
            if($v['parent_id']==$parent_id){
                $data[]=$v['id'];
                $this->_getCatChild($info,$v['id']);
            }
        }
        return $data;
    }

    //检验权限
    public function chkPri($admin_id,$m,$c,$a)
    {
        $has = Db::table('privilege')
            ->alias('p')
            ->join('role_pri rp','rp.pri_id = p.id','LEFT')
            ->join('admin_role ar','ar.role_id = rp.role_id')
            ->where(['ar.admin_id'=>$admin_id,'p.module_name'=>$m,'p.controller_name'=>$c,'p.action_name'=>$a,'is_usable'=>1])
            ->count();
        return ($has >0);
    }



    //获取管理员的权限
    public function getPriByAdminId($admin_id)
    {
        $info = Db::table('privilege')
            ->alias('p')
            ->field('DISTINCT p.id,p.pri_name,p.module_name,p.controller_name,p.action_name,p.parent_id,p.ico')
            ->join('role_pri rp','rp.pri_id = p.id','LEFT')
            ->join('admin_role ar','ar.role_id = rp.role_id')
            ->where(['ar.admin_id'=>$admin_id,'p.is_usable'=>1,'p.is_show'=>1])
            ->order('p.sort_num')
            ->select();
        return $info;
    }

    //获取左侧菜单按钮
    public function getBtn()
    {
        $admin_id = session('admin_id');
        if($admin_id == 1){//超级管理员
            $info=Db::table('privilege')->where(['is_usable'=>1,'is_show'=>1])->order('sort_num')->select();
        }else{
            $info=$this->getPriByAdminId($admin_id);
        }
        $_info=array();
        foreach($info as $k=>$v){
            if($v['parent_id']==0){
                foreach($info as $k1=>$v1){
                    if($v1['parent_id']==$v['id']){
                        $_info[$v['pri_name'].'_'.$v['ico']][]=$v1;
//                        $_info['children'][]=$v1;
                    }
                }
            }
        }
        return $_info;
    }


}
